﻿using System;
using System.IO;
using System.Windows.Forms;

namespace LinkGame
{
    public partial class FrmSetting : Form
    {
        #region Private Fields

        private readonly Setting _setting;

        #endregion Private Fields

        #region Public Constructors

        public FrmSetting(Setting setting)
            : this()
        {
            _setting = setting;
        }

        #endregion Public Constructors

        #region Private Constructors

        private FrmSetting()
        {
            InitializeComponent();
        }

        #endregion Private Constructors

        #region Public Properties

        public Setting Setting => _setting;

        #endregion Public Properties

        #region Protected Methods

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            cbIconSets.Items.Clear();
            var iconSets = Directory.EnumerateFiles("Icons", "*.zip");
            foreach (var iconSet in iconSets)
            {
                cbIconSets.Items.Add(iconSet);
            }

            if (!string.IsNullOrEmpty(_setting.IconSet))
            {
                cbIconSets.SelectedItem = _setting.IconSet;
            }
            else
            {
                cbIconSets.SelectedIndex = 0;
            }
            
        }

        #endregion Protected Methods

        #region Private Methods

        private void btnOK_Click(object sender, EventArgs e)
        {
            _setting.IconSet = (string)cbIconSets.SelectedItem;
        }

        private void cbIconSets_SelectedIndexChanged(object sender, EventArgs e)
        {
            var keys = Utils.LoadIconSet(imageList1, (string)cbIconSets.SelectedItem);
            lvIcons.Items.Clear();
            foreach (var key in keys)
            {
                lvIcons.Items.Add(key, key, key);
            }
        }

        #endregion Private Methods
    }
}